package geor.app;

import java.util.Calendar;
import java.util.GregorianCalendar;

import lib.util.DateTimeUtils;
import lib.util.NumberUtils;
import lib.view.FullActivity;
import geor.app.R;
import geor.controller.OpcoesPagamento;
import geor.model.ReceitaHelper;
import geor.view.ListaRelatoriosActivity;
import geor.view.SalvarReceitaActivity;
import geor.view.VerComprasActivity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends FullActivity {

	private Button btVerCompras = null, btNovaCompra = null;
	private String mesAno = null;
	private TextView receitaAtual = null;
	private TextView contasMes = null;
	private TextView contasPagar = null;
	private TextView saldoMesSeguinte = null;
	private TextView viewMesAno = null;
	private Calendar calendar = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		helper = new ReceitaHelper(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		load();
		set();
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		new MenuInflater(this).inflate(R.menu.main_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_nova_receita) {
			go(SalvarReceitaActivity.class);
			return true;
		} else if (item.getItemId() == R.id.menu_relatorios) {
			go(ListaRelatoriosActivity.class);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void populate(ContentValues values) {
	}

	@Override
	public void createOrUpdate(ContentValues values) {
	}

	@Override
	public boolean delete(String id) {
		return false;
	}

	@Override
	public boolean validate(ContentValues values) {
		return false;
	}

	@Override
	public void load() {
		btVerCompras = (Button) findViewById(R.id.bt_ver_compras);
		btNovaCompra = (Button) findViewById(R.id.bt_nova_compra);
		
		calendar = new GregorianCalendar();
		mesAno = DateTimeUtils.formatDate(calendar.getTime(), DateTimeUtils.MES_ANO);
		
		viewMesAno = (TextView) findViewById(R.id.mes_ano);
		viewMesAno.setText(mesAno);
		
		receitaAtual = (TextView) findViewById(R.id.receita_atual);
		Cursor ra = helper.read("1");
		ra.moveToFirst();
		receitaAtual.setText(NumberUtils.toRealFormat(ra.getDouble(2)));
		
		contasMes = (TextView) findViewById(R.id.contas_mes);
		Cursor cm = helper.contas_Mes(DateTimeUtils.formatDate(calendar.getTime(), DateTimeUtils.DEF_MES_ANO));
		cm.moveToFirst();
		contasMes.setText(NumberUtils.toRealFormat(cm.getDouble(0)));
		
		contasPagar = (TextView) findViewById(R.id.contas_pagar);
		Cursor cp = helper.contas_Pagar(DateTimeUtils.formatDate(calendar.getTime(), DateTimeUtils.DEF_MES_ANO));
		cp.moveToFirst();
		contasPagar.setText(NumberUtils.toRealFormat(cp.getDouble(0)));
		
		saldoMesSeguinte = (TextView) findViewById(R.id.mes_seguinte);
		saldoMesSeguinte.setText(NumberUtils.toRealFormat(ra.getDouble(2) - cp.getDouble(0)));
		
		cp.close();
		cm.close();
		ra.close();
		
	}

	@Override
	public void set() {
		btVerCompras.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				go(VerComprasActivity.class);
			}
		});

		btNovaCompra.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// go(SalvarCompraVistaActivity.class);
				AlertDialog.Builder opcoes = new AlertDialog.Builder(
						MainActivity.this);
				final String lista[] = { "À Vista", "A Prazo" };
				opcoes.setTitle("Opções de pagamento");
				opcoes.setItems(lista, new OpcoesPagamento(MainActivity.this));
				// Setting Netural "Cancel" Button
				opcoes.setNeutralButton("Cancelar", null);
				opcoes.show();
			}
		});
	}
}